/**
 * @(#) CongMoneyMediator.as
 *
 * @date		2009. 03. 25.
 * @version		1.0
 * @author		baek, kwang hyoun
 * @description	건강보험증 신청 - Mediator
 * 				건강보험증을 조회하고 신청하는 Presentation Logic을 관리
 */
package com.skt.common_hr.bankreq.view
{
	import com.skt.common_hr.bankreq.BankreqFacade;
	import com.skt.common_hr.bankreq.model.AttachFileProxy;
	import com.skt.common_hr.bankreq.model.BankreqProxy;
	import com.skt.common_hr.bankreq.model.vo.BankReqFileUpLoadVO;
	import com.skt.common_hr.bankreq.view.components.ReqBankreqViewSac;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	import com.wcl.utils.StringUtil;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class reqBankreqViewSacMediator extends OverViewMediator
	{		
		public static const NAME:String = "reqbankreqMediator";
		private var _bWaitingSave:Boolean = false;
		private var orgFileName:String = "";

		/**
		* 생성자
		*
		* @param
		* @return void
		*/
        public function reqBankreqViewSacMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }

		/***
		 * 활성화가 될때마다 불림.
		 */
		override protected function activatedView():void
		{
			super.activatedView();
			clearView();
		}

 		override protected function clearView():void
 		{
 			//view.vsCompensation.selectedIndex = 0;
 			//view.tb.selectedIndex = 0;
			//loadProfile();
 		}
 		
		override protected function closedView():void
		{
			removeEventListener();
		}
		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		* 최초 한번만 불림
		*/
 		override protected function initializeView():void
 		{
			super.initializeView();
			setEventListener();
 		}
 		
 		
 		private function setEventListener():void
		{ 
			//facade.registerProxy(new AttachFileProxy());
 			view.addEventListener(ReqBankreqViewSac.GetFile, onBrowseFile);
 			view.addEventListener(ReqBankreqViewSac.NoticeSave, onSave);
 			view.addEventListener(ReqBankreqViewSac.NoticeOnlyFileDelete, onNoticeOnlyFileDelete);
 		}
 		
 		private function onBrowseFile(event:Event):void
		{
			
			var proxy:AttachFileProxy = facade.retrieveProxy(AttachFileProxy.NAME) as AttachFileProxy;
			proxy.browse();
		} 
		
		private function onSelectedFile():void
		{
			var proxy:AttachFileProxy = facade.retrieveProxy(AttachFileProxy.NAME) as AttachFileProxy;
			view.fileName = proxy.fileVo.I_FILE_NAME;
		}



 		private function removeEventListener():void
 		{
//			view.removeEventListener(HthIcFacade.HthIcWrite, onClickNew);
//			view.removeEventListener(gwarekeyEvent.SELECT_DATA, onClickReadOnly);
//			view.removeEventListener(gwarekeyEvent.UPDATE_DATA, onClickModify);
//			view.removeEventListener(gwarekeyEvent.DELETE_DATA, onClickDelete);
//			view.removeEventListener(gwarekeyEvent.PRINT_DATA, onStatementView);
 		}


 		private function loadList():void
 		{
// 			var mProxy:MedicalMoneyProxy = facade.retrieveProxy(MedicalMoneyProxy.NAME) as MedicalMoneyProxy;
//			mProxy.loadList(Variables.myPernr);
 		}


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		*
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array
		{
			return basicInterestMessages.concat([
					BankreqFacade.INSERT_COMPLETE,
					AttachFileProxy.SELECTED_FILE,
					AttachFileProxy.FAILED_UPLOAD,
					AttachFileProxy.COMPLETE_UPLOAD,
					ReqBankreqViewSac.WRITE_VIEW_RECEIVE
					
					// reload
//					ReqMedicalMoneyMediator.CLOSED_REQUEST_VIEW,
//					MedicalMoneyProxy.LOAD_MEDICALMONEY_LIST_COMPLETE,
//					MedicalMoneyProxy.LOAD_MEDICALMONEY_LIST_FAILED
					]);
		}


		/**
		* Noti별로 처리한다.
		*
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName())
			{
				case BankreqFacade.INSERT_COMPLETE:					
					view._save();
					break;
				case AttachFileProxy.SELECTED_FILE:
					onSelectedFile();
					break;
				case AttachFileProxy.COMPLETE_UPLOAD:
					onCompleteUpload(note.getBody() as AttachFileProxy);
					break;
				case AttachFileProxy.FAILED_UPLOAD:
					onFailedUpload(note.getBody() as AttachFileProxy);
					break;					

				case ReqBankreqViewSac.WRITE_VIEW_RECEIVE:
					fileInfo = new BankReqFileUpLoadVO();
					//setEventListener();
					orgFileName = "";
					view._dataProvider = null;
					view.tiFileName.text = "";
					view.fileName = '';
					view.txtComment.text = '';
					view.txtStatus.text  = '';
					view.txtSinChung.text = '';
					view.txtNum.text = '';
					//view.dataproviders = null;
					break;										
					
				// 의료비 리스트  조회 Proxy 호출결과 처리
//				case MedicalMoneyProxy.LOAD_MEDICALMONEY_LIST_COMPLETE :
//					setListView(MedicalMoneyProxy(note.getBody()).mainList);
//					break;
//				// 의료비 리스트 조회 실패시
//				case MedicalMoneyProxy.LOAD_MEDICALMONEY_LIST_FAILED :
//					CommonAlert.showMessage(commonFacade.bundle, "ALERT_FAILED_LOAD_LIST", null, "ALERT_TITLE_SYSTEM_ERROR");
//					break;
//
//				// 재조회
//				case ReqMedicalMoneyMediator.CLOSED_REQUEST_VIEW:
//					view.vsCompensation.selectedIndex = 0;
//					loadList();
//					break;

				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}


 		//private var vo:NoticeDetailVO = new NoticeDetailVO();
 		private function onBankReqSave(event:Event):void
 		{
// 			if(!validateFields()) {
// 				return;
// 			}
 			if(StringUtil.NVL(view.fileName)!="" && StringUtil.NVL(view.fileName)!=StringUtil.NVL(orgFileName)) {
 				_bWaitingSave = true;
				var proxy:AttachFileProxy = facade.retrieveProxy(AttachFileProxy.NAME) as AttachFileProxy;
				proxy.uploadFile();
 			}
 			else {
 				if(StringUtil.NVL(orgFileName)!="" && StringUtil.NVL(view.fileName)!=StringUtil.NVL(orgFileName)) {
 					//view.deleteFile();
 				}
 				view.save();
 			}
 		}

		private var fileInfo:BankReqFileUpLoadVO;
		
		private function onCompleteUpload(proxy:AttachFileProxy):void
		{
			trace("onCompleteUpload: " + proxy.fileVo.I_FILE_URL + "/" + proxy.fileVo.I_FILE_NAME);
			fileInfo = proxy.fileVo;
			saveNotice();
		}
		
		private function onFailedUpload(proxy:AttachFileProxy):void
		{
			trace("onFailedUpload: " + proxy.fileVo.I_FILE_NAME);
			CommonAlert.showMessage(commonFacade.bundle, "ALERT_ERROR_UPLOAD"); 
			fileInfo = null;
		}
		
 		/**
		 * Proxy에서 DataLoad를 완료한 후 호출됨
		 *     - load된 데이터를 화면에 출력
		 * @param
		 * @return void
		 */
 		private function setListView(list:ArrayCollection):void
 		{
// 			view.medicalMoneyDataList = list;
// 			commonFacade.setStatusMessage("STATUS_LOADED_LIST", [list.length.toString()]);
// 			if(list.length == 0) {
// 				view.showInitialTip();
// 			}
 		}

 		//private var vo:NoticeDetailVO = new NoticeDetailVO();
 		private function onSave(event:Event):void
 		{
 			if(StringUtil.NVL(view.fileName)!="" && StringUtil.NVL(view.fileName)!=StringUtil.NVL(orgFileName)) {
 				_bWaitingSave = true;
				var proxy:AttachFileProxy = facade.retrieveProxy(AttachFileProxy.NAME) as AttachFileProxy;
				proxy.uploadFile();
 			}
 			else {
 				if(StringUtil.NVL(orgFileName)!="" && StringUtil.NVL(view.fileName)!=StringUtil.NVL(orgFileName)) {
 					//deleteFile();
 				}
 				saveNotice();
 			}
 		}

		private function saveNotice():void
		{
			_bWaitingSave = false;
			var proxy:BankreqProxy = facade.retrieveProxy(BankreqProxy.NAME) as BankreqProxy;
 			
 			var param:Object = {};
			param.I_PERNR = Variables.myPernr;
			param.I_GUBUN = view.gubun;
			param.I_ACTIO = view._mode;

			var itab:Object = {};
			itab.BEGDA = view.begda;
			itab.ENDDA = view.endda;
			itab.BANKL = view.cbBankType.selectedItem.CODE;
			itab.BANKN = view.txtNum.text;
			itab.SREASN = view.txtSinChung.text; 
			
			if(fileInfo!=null) {
				itab.FILE_SIZE 		= fileInfo.I_FILE_SIZE;
				itab.FILE_NAME   = fileInfo.I_FILE_NAME;
				itab.FILE_EXT 		= fileInfo.I_FILE_EXT;
				itab.FILE_URL 		= fileInfo.I_FILE_URL;				
			}

			
			proxy.save(param, itab);			
		}

		//파일삭제
		private function onNoticeOnlyFileDelete(event:Event):void
		{
			fileInfo = null;
			view.fileName = "";
		}



       private function get view():ReqBankreqViewSac
        {
            return viewComponent as ReqBankreqViewSac;
        }


	}
}